import * as types from "./mutation-types";
import Api from "api/api";

const state = {
  roleList: {},
  currentRoleList: [], //当前角色id
  roleView: {}, //角色详情
  roleUserList: {}, // 角色下属用户
  roleDataUserList: {}, // 数据角色下属用户
  search: {
    deptId: "",
    endTime: "",
    startTime: "",
    pageSize: 10,
    name: ""
  },
  userInfoView: {}
};

const getters = {
  roleList: state => state.roleList,
  roleView: state => state.roleView,
  currentRoleList: state => state.currentRoleList,
  roleUserList: state => state.roleUserList,
  roleDataUserList: state => state.roleDataUserList,
  search: state => state.search,
  userInfoView: state => state.userInfoView
};

const actions = {
  //获取角色列表
  roleListUsingPOSTS({ commit }, param) {
    return Api.getRolePageList(param).then(ret => {
      commit(types.UPDATE_ROLE_LIST, ret.data);
      return ret;
    });
  },
  //角色当前资源(返回所有资源id)
  roleResListS({ commit }, param) {
    return Api.roleResList(param).then(ret => {
      commit(types.UPDATE_CURRENT_ROLE_LIST, ret.data);
      return ret;
    });
  },
  //角色权限资源保存
  ResRoleSaveUsingPOSTS({ commit }, param) {
    return Api.distributeRole(param).then(ret => {
      commit(types.INIT_CURRENT_ROLE_LIST, ret.data);
      return ret;
    });
  },
  //角色详情列表
  roleViewUsingPOSTS({ commit }, param) {
    return Api.getRoleById(param).then(ret => {
      commit(types.UPDATE_ROLE_VIEW, ret.data);
      return ret;
    });
  },
  //角色保存
  roleSaveUsingPOSTS({ commit }, param) {
    return Api.addOrUpdateRole(param).then(ret => {
      commit(types.INIT_ROLE_VIEW, ret.data);
      return ret;
    });
  },
  //角色删除
  roleDelUsingPOSTS({ commit }, param) {
    return Api.deleteRoleById(param).then(ret => {
      return ret;
    });
  },
  //角色下属用户
  roleUserListUsingPOSTS({ commit }, param) {
    return Api.getRoleUserList(param).then(ret => {
      commit(types.UPDATE_ROLE_USER_LIST, ret.data);
      return ret;
    });
  }
};

const mutations = {
  [types.UPDATE_ROLE_LIST](state, payload) {
    state.roleList = payload;
  },
  [types.UPDATE_INFO_VIEW](state, payload) {
    state.userInfoView = payload;
  },
  [types.UPDATE_CURRENT_ROLE_LIST](state, payload) {
    state.currentRoleList = payload;
  },
  [types.UPDATE_ROLE_VIEW](state, payload) {
    state.roleView = payload;
  },
  [types.UPDATE_ROLE_USER_LIST](state, payload) {
    state.roleUserList = payload;
  },
  [types.INIT_DATA_ROLE_LIST](state, payload) {
    state.roleDataUserList = payload;
  },
  [types.INIT_ROLE_VIEW](state, payload) {
    state.roleView = {
      name: "",
      description: ""
    };
  },
  [types.INIT_ROLE_SEARCH](state, payload) {
    state.search = {
      deptId: "",
      endTime: "",
      startTime: "",
      pageSize: 10,
      name: ""
    };
  },

  [types.INIT_CURRENT_ROLE_LIST](state) {
    state.currentRoleList = [];
  }
};

export default {
  state,
  getters,
  actions,
  mutations
};
